package com.acc.sys.framework;

import apijson.NotNull;
import apijson.StringUtil;
import apijson.framework.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AccApplication extends APIJSONApplication {
    static {
        // 使用本项目的自定义处理类
        AccApplication.DEFAULT_APIJSON_CREATOR = new AccCreator();
    }

    /**
     * 初始化，加载所有配置并校验
     *
     * @param shutdownWhenServerError
     * @return
     * @throws Exception
     */
    public static void init(boolean shutdownWhenServerError) throws Exception {
        init(shutdownWhenServerError, DEFAULT_APIJSON_CREATOR);
    }


    /**
     * 初始化，加载所有配置并校验
     *
     * @param shutdownWhenServerError
     * @param creator
     * @return
     * @throws Exception
     */
    public static <T extends Object> void init(boolean shutdownWhenServerError, @NotNull APIJSONCreator<T> creator) throws Exception {
        System.out.println("\n\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 开始启动 >>>>>>>>>>>>>>>>>>>>>>>>\n");
        DEFAULT_APIJSON_CREATOR = creator;

        // 统一用同一个 creator
        APIJSONSQLConfig.APIJSON_CREATOR = creator;
        APIJSONParser.APIJSON_CREATOR = creator;
        APIJSONController.APIJSON_CREATOR = creator;


        if (APIJSONVerifier.ENABLE_VERIFY_ROLE) {
            System.out.println("\n\n\n开始初始化: Access 权限校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
            try {
                AccVerifier.initAccess(shutdownWhenServerError, creator,null);
            } catch (Throwable e) {
                log.error(e.getMessage());
                if (shutdownWhenServerError) {
                    onServerError("Access 权限校验配置 初始化失败！", shutdownWhenServerError);
                }
            }
            System.out.println("\n完成初始化: Access 权限校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }
        if (APIJSONFunctionParser.ENABLE_REMOTE_FUNCTION) {
            System.out.println("\n\n\n开始初始化: Function 远程函数配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
            try {
                AccFunctionParser.init(shutdownWhenServerError, creator,null);
            } catch (Throwable e) {
                log.error(e.getMessage());
                if (shutdownWhenServerError) {
                    onServerError("Function 远程函数配置 初始化失败！", shutdownWhenServerError);
                }
            }
            System.out.println("\n完成初始化: Function 远程函数配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

            System.out.println("开始测试: Function 远程函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
            try {
                APIJSONFunctionParser.test();
            } catch (Throwable e) {
                log.error(e.getMessage());
                if (shutdownWhenServerError) {
                    onServerError("Function 远程函数配置 测试失败！", shutdownWhenServerError);
                }
            }
            System.out.println("\n完成测试: Function 远程函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }


        if (APIJSONVerifier.ENABLE_VERIFY_CONTENT) {
            System.out.println("\n\n\n开始初始化: Request 请求参数校验配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
            try {
                APIJSONVerifier.initRequest(shutdownWhenServerError, creator);
            } catch (Throwable e) {
                log.error(e.getMessage());
                if (shutdownWhenServerError) {
                    onServerError("Request 请求参数校验配置 初始化失败！", shutdownWhenServerError);
                }
            }
            System.out.println("\n完成初始化: Request 请求参数校验校验配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

            System.out.println("\n\n\n开始测试: Request 请求参数校验 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
            try {
                APIJSONVerifier.testStructure();
            } catch (Throwable e) {
                log.error(e.getMessage());
                if (shutdownWhenServerError) {
                    onServerError("Request 请求参数校验 测试失败！", shutdownWhenServerError);
                }
            }
            System.out.println("\n完成测试: Request 请求参数校验 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }
        System.out.println("\n\n<<<<<<<<<<<<<<<<<<<<<<<<< APIJSON 启动完成，试试调用零代码万能通用 API 吧 ^_^ >>>>>>>>>>>>>>>>>>>>>>>>\n");
    }


}
